export const circularReporting1: (
    number: number,
    loopNumber: number
) => number = (number, loopNumber) => {
    const arr = new Array(number).fill(number).map((_, index) => ++index)
    let start = 1 // 报数
    let index = 0 // 索引
    let leave = 0 // 已经离开的人数
    while (leave < arr.length - 1) {
        if (arr[index] === -1) {
            index = ++index % arr.length
            continue
        }
        if (start === 0) {
            arr[index] = -1
            leave++
        }
        index = ++index % arr.length
        start = ++start % loopNumber
    }
    return arr.find((item) => item >= 0) as number
}
export const circularReporting2: (
    number: number,
    loopNumber: number
) => number = (number, loopNumber) => {
    let arr = new Array(number).fill(number).map((_, index) => ++index)
    let start = 0 // 报数
    while (arr.length > 1) {
        for (let i = 0; i < arr.length; i++) {
            start = ++start % loopNumber
            if (start === 0) arr[i] = -1
        }
        arr = arr.filter((item) => item >= 0)
    }
    return arr[0]
}
export const circularReporting3: (
    number: number,
    loopNumber: number
) => number = (number, loopNumber) => {
    let allplayer: number[] = []
    for (let i = 0; i < number; i++) {
        allplayer[i] = i + 1
    }

    let exitCount = 0 // 离开人数
    let counter = 0 // 记录报数
    let curIndex = 0 // 当前下标

    while (exitCount < number - 1) {
        if (allplayer[curIndex] !== 0) counter++

        if (counter == loopNumber) {
            allplayer[curIndex] = 0
            counter = 0
            exitCount++
        }
        curIndex++
        if (curIndex == number) {
            curIndex = 0
        }
    }
    for (let i = 0; i < number; i++) {
        if (allplayer[i] !== 0) {
            return allplayer[i]
        }
    }
    return -1
}
